package org.zeroturnaround.javarebel.integration.jmonkey;

import org.zeroturnaround.javarebel.*;


public class JMonkeyPlugin
        implements Plugin {

    private static final String CLASS_BASESIMPLEGAME = "com.jme.app.BaseSimpleGame";

    public JMonkeyPlugin() {
        super();
    }

    public void preinit() {
        Integration i = IntegrationFactory.getInstance();
        ClassLoader cl = JMonkeyPlugin.class.getClassLoader();
        i.addIntegrationProcessor(cl, CLASS_BASESIMPLEGAME, new JMonkeyBaseClassBytecodeProcessor());
        ReloaderFactory.getInstance().addClassReloadListener(new ClassEventListener() {
            final JMonkeyPlugin this$0;

            {
                this$0 = JMonkeyPlugin.this;
            }

            public void onClassEvent(int eventType, Class klass) {
                try {
                    Class abstractGameClass = Class.forName(CLASS_BASESIMPLEGAME);
                    if (abstractGameClass.isAssignableFrom(klass)) {
                        JMonkeyAppConfigReloader.reinitialize(klass);
                        LoggerFactory.getInstance().echo("Reinitialize jmonkey...");
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                    LoggerFactory.getInstance().error(e);
                    System.out.println(e);
                }
            }

            public int priority() {
                return 0;
            }

        }
        );
    }

    public boolean checkDependencies(ClassLoader classLoader, ClassResourceSource classResourceSource) {
        return classResourceSource.getClassResource(CLASS_BASESIMPLEGAME) != null;
    }

    public String getId() {
        return "monkey_plugin";
    }

    public String getName() {
        return "JRebel Jmonkey Plugin ";
    }

    public String getDescription() {
        return "Reload instance configuration for the JRebel SDK demo-app.";
    }

    public String getAuthor() {
        return "Arkadiusz Czarnik";
    }

    public String getWebsite() {
        return null;
    }
}
